<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增厂家信息档案')" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: summernote-css" />
    <link href="../static/css/bootstrap-select.min.css" th:href="@{/css/bootstrap-select.min.css}" rel="stylesheet"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-factory-add">
            <input id="factoryProvinceName" name="factoryProvinceName" class="form-control" type="hidden">
            <input id="factoryCityName" name="factoryCityName" class="form-control" type="hidden">
            <input id="factoryDistrictName" name="factoryDistrictName" class="form-control" type="hidden">
            <h4 class="form-header h4">基本信息</h4>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">厂家名称：</label>
                        <div class="col-sm-8">
                            <input name="factoryName" autocomplete="off" class="form-control" type="text">
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">厂家登录密码：</label>
                        <div class="col-sm-8">
                            <div class="input-group">
                                <input id="factoryPassword" name="factoryPassword" placeholder="请输入登录密码" class="form-control" type="password" th:value="${@config.getKey('sys.user.initPassword')}" required>
                                <span class="input-group-addon" title="登录密码,鼠标按下显示密码"
                                      onmousedown="$('#factoryPassword').attr('type','text')"
                                      onmouseup="$('#factoryPassword').attr('type','password')"><i class="fa fa-key"></i></span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">厂家负责人：</label>
                        <div class="col-sm-8">
                            <input name="factoryPerson" autocomplete="off" class="form-control" type="text">
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">负责人手机号：</label>
                        <div class="col-sm-8">
                            <input id="factoryPhone" autocomplete="off" name="factoryPhone" class="form-control" type="text">
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">虚拟厂家名称：</label>
                        <div class="col-sm-8">
                            <input name="factoryNameVirtual" autocomplete="off" class="form-control" type="text">
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">虚拟手机号：</label>
                        <div class="col-sm-8">
                            <input id="factoryPhoneVirtual" autocomplete="off" name="factoryPhoneVirtual" class="form-control" type="text">
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <label class="col-sm-4 control-label is-required">所属区域：</label>
                    <div class="col-sm-8">
                        <select class="selectpicker form-control" onchange="initCity()" id="factoryProvice" name="factoryProvice" required>
                            <option value="">&#45;&#45;省份&#45;&#45;</option>
                        </select>
                        <select class="selectpicker form-control" onchange="initRegion()" id="factoryCity" name="factoryCity" required>
                            <option value="">&#45;&#45;城市&#45;&#45;</option>
                        </select>
                        <select class="selectpicker form-control" id="factoryDistrict" name="factoryDistrict" required>
                            <option value="">&#45;&#45;县区&#45;&#45;</option>
                        </select>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">厂家详细地址：</label>
                        <div class="col-sm-8">
                            <input name="factoryAddress" autocomplete="off" class="form-control" type="text">
                        </div>
                    </div>
                </div>
            </div>
            <!--<div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">厂家账户余额：</label>
                        <div class="col-sm-8">
                            <input name="factoryBalance" class="form-control" type="text">
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">厂家账户保证金：</label>
                        <div class="col-sm-8">
                            <input name="factoryMoney" class="form-control" type="text">
                        </div>
                    </div>
                </div>
            </div>-->
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">抽成比例：</label>
                        <div class="col-sm-8">
                            <input name="factoryProportion" autocomplete="off" class="form-control" type="text">
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">邮箱：</label>
                        <div class="col-sm-8">
                            <input id="factoryEmail" autocomplete="off" name="factoryEmail" class="form-control" type="text">
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">厂家合作产品：</label>
                        <div class="col-sm-8">
                            <textarea name="factoryProducts" autocomplete="off" class="form-control"></textarea>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">备注：</label>
                        <div class="col-sm-8">
                            <textarea name="factoryRemarks" autocomplete="off" class="form-control"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">合同约定质保金额：</label>
                        <div class="col-sm-8">
                            <input name="factoryContractAmount" autocomplete="off" class="form-control" type="text">
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">是否显示电话：</label>
                        <div class="col-sm-8">
                            <select name="isShowPhone" class="form-control m-b" th:with="type=${@dict.getType('zsyh_yesno')}" required>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                            </select>
                        </div>
                    </div>
                </div>

            </div>


            <h4 class="form-header h4">其他信息</h4>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">厂家用户协议：</label>
                        <div class="col-sm-8">
                            <input type="hidden" class="form-control" name="factoryProtocol">
                            <div class="summernote" id="factoryProtocol"></div>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">营业执照：</label>
                        <div class="col-sm-8">
                            <input type="hidden" name="factoryLicence">
                            <div class="file-loading">
                                <input class="form-control file-upload" id="factoryLicence" name="file" type="file">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <div class="row">
        <div class="col-sm-offset-5 col-sm-10">
            <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;
            <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <th:block th:include="include :: summernote-js" />
    <script src="../static/js/bootstrap-select.min.js" th:src="@{/js/bootstrap-select.min.js}"></script>
    <script th:inline="javascript">
        var prefix = ctx + "zsyh/factory"
        $("#form-factory-add").validate({
            onkeyup: false,
            rules:{
                factoryPassword:{
                    minlength: 5,
                    maxlength: 20
                },
                /*factoryEmail:{
                    email:true,
                    remote: {
                        url: prefix + "/checkEmailUnique",
                        type: "post",
                        dataType: "json",
                        data: {
                            "factoryEmail": function () {
                                return $.common.trim($("#factoryEmail").val());
                            }
                        },
                        dataFilter: function (data, type) {
                            return $.validate.unique(data);
                        }
                    }
                },*/
                factoryPhone:{
                    isPhone:true,
                    remote: {
                        url: prefix + "/checkPhoneUnique",
                        type: "post",
                        dataType: "json",
                        data: {
                            "factoryPhone": function () {
                                return $.common.trim($("#factoryPhone").val());
                            }
                        },
                        dataFilter: function (data, type) {
                            return $.validate.unique(data);
                        }
                    }
                },
            },
            messages: {
                "factoryEmail": {
                    remote: "Email已经存在"
                },
                "factoryPhone":{
                    remote: "手机号码已经存在"
                }
            },
            focusCleanup: true
        });

        $(document).ready(function(){
            // 地区下拉框初始化
            $("#factoryProvice").selectpicker({width:130, liveSearch: true, liveSearchPlaceholder: "搜索" });
            $("#factoryCity").selectpicker({width:120, liveSearch: true, liveSearchPlaceholder: "搜索" });
            $("#factoryDistrict").selectpicker({width:120, liveSearch: true, liveSearchPlaceholder: "搜索" });

            // 动态加载省份选项数据
            initSelectOptions("factoryProvice","0");
        });// onReady

        /**
         * 动态生成select选项
         * @param selectId
         * @returns
         */
        function initSelectOptions(selectId,parentId) {
            var selectObj = $("#" + selectId);
            $.ajax({
                url : ctx + "zsyh/factory/"+parentId,
                async : false,
                type : "GET",
                success : function(result) {
                    debugger;
                    var configs = result.rows;
                    selectObj.find("option:not(:first)").remove();
                    for (var i in configs) {
                        var addressConfig = configs[i];
                        var optionValue = addressConfig.id;
                        var optionText = addressConfig.areaName;
                        selectObj.append(new Option(optionText, optionValue));
                    }

                    // 刷新select
                    selectObj.selectpicker('refresh');
                },
                error : function(result) {
                    toastr.error('获取['+ parentId + ']信息失败，原因：' + result.errorMessage);
                }
            });// ajax
        }

        /**
         * 根据选择的省份动态初始化城市options
         * @returns
         */
        function initCity() {
            // 当省份变动时，初始化城市和清空县区和街道
            var provinceSel = $("#factoryProvice").val();
            initSelectOptions("factoryCity", provinceSel);
            $("#factoryDistrict").find("option:not(:first)").remove();
            $("#factoryDistrict").selectpicker('refresh');
        }

        /**
         * 根据选择的城市动态初始化区域options
         * @returns
         */
        function initRegion() {
            // 选择城市
            var citySel = $("#factoryCity").val();
            initSelectOptions("factoryDistrict", citySel);
        }

        function submitHandler() {
            if ($.validate.form()) {
                $("#factoryProvinceName").val($('#factoryProvice option:selected').text());
                $("#factoryCityName").val($('#factoryCity option:selected').text());
                $("#factoryDistrictName").val($('#factoryDistrict option:selected').text());
                $.operate.save(prefix + "/add", $('#form-factory-add').serialize());
            }
        }

        $(".file-upload").fileinput({
            uploadUrl: ctx + 'common/uploadOSS',
            maxFileCount: 1,
            showUpload:false,
            autoReplace: true
        }).on('fileuploaded', function (event, data, previewId, index) {
            $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
        }).on('fileremoved', function (event, id, index) {
            $("input[name='" + event.currentTarget.id + "']").val('')
        }).on("filebatchselected", function(event, files) {
            $(this).fileinput("upload");
        })

        $(function() {
            $('.summernote').summernote({
                lang: 'zh-CN',
                callbacks: {
                    onChange: function(contents, $edittable) {
                        $("input[name='" + this.id + "']").val(contents);
                    },
                    onImageUpload: function(files) {
                        var obj = this;
                        var data = new FormData();
                        data.append("file", files[0]);
                        $.ajax({
                            type: "post",
                            url: ctx + "common/uploadOSS",
                            data: data,
                            cache: false,
                            contentType: false,
                            processData: false,
                            dataType: 'json',
                            success: function(result) {
                                if (result.code == web_status.SUCCESS) {
                                    $('#' + obj.id).summernote('insertImage', result.url);
                                } else {
                                    $.modal.alertError(result.msg);
                                }
                            },
                            error: function(error) {
                                $.modal.alertWarning("图片上传失败。");
                            }
                        });
                    }
                }
            });
        });

    </script>
</body>
</html>